/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */

/*
 * @version client_test.c
 * @package Comoro
 * @author Dr.NP <np@bsgroup.org>
 * 
 * Heat bench test
 */

#include "client_test.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <event.h>

#ifdef HAVE_MALLOC_H
#ifndef __OpenBSD__
#include <malloc.h>
#endif
#endif

struct event_base *ev_base;
int conn_pool[MAX_CONNECTION];
static struct event hb_event;
static struct event conn_event;
int total_connections = 0;
int max_connections = 0;

/* Clock handler, create new connection */
void conn_clock_handler(const int fd, const short which, void *arg)
{
	struct timeval t = {.tv_sec = 0, .tv_usec = 20};
	static char initialized = 0x0;
	
	if (initialized)
	{
		evtimer_del(&conn_event);
	}
	
	else
	{
		initialized = 0x1;
	}
	
	if (total_connections >= max_connections)
	{
		return;
	}
	
	evtimer_set(&conn_event, conn_clock_handler, 0);
	event_base_set(ev_base, &conn_event);
	evtimer_add(&conn_event, &t);
	
	// Create connection
	int cfd = create_conn(inet_addr(TEST_SRV_ADDR), TEST_SRV_PORT);
	if (cfd > 0)
	{
		fprintf(stderr, "Connection [%5d] created...", total_connections);
		// Add to pool
		conn_pool[total_connections] = cfd;
		total_connections ++;
	}
}

/* Clock handler, send heartbeat signal to comoro server */
void hb_clock_handler(const int fd, const short which, void *arg)
{
	struct timeval t = {.tv_sec = 1, .tv_usec = 0};
	static char initialized = 0x0;
	static unsigned char hb_signal[8] = {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00};
	
	if (initialized)
	{
		evtimer_del(&hb_event);
	}
	
	else
	{
		initialized = 0x1;
	}
	
	evtimer_set(&hb_event, hb_clock_handler, 0);
	event_base_set(ev_base, &hb_event);
	evtimer_add(&hb_event, &t);
	
	// Send heartbeat
	int c;
	
	for (c = 0; c < MAX_CONNECTION; c ++)
	{
		if (conn_pool[c] > 0)
		{
			send(conn_pool[c], hb_signal, 8, 0);
		}
	}
	
	fprintf(stderr, "Sending heartbeat...\n");
	
	return;
}

/* Create a connection */
int create_conn(unsigned long srv_addr, int srv_port)
{
	int fd;
	struct sockaddr_in cnt_addr;
	
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd < 0)
	{
		fprintf(stderr, "Socket error\n");
		exit(-1);
	}
	
	memset(&cnt_addr, 0, sizeof(cnt_addr));
	cnt_addr.sin_family = AF_INET;
	cnt_addr.sin_addr.s_addr = srv_addr;
	cnt_addr.sin_port = htons(srv_port);
	
	if (connect(fd, (struct sockaddr *) &cnt_addr, sizeof(cnt_addr)) < 0)
	{
		fprintf(stderr, "Connect to center %lu:%d server failed\n", srv_addr, srv_port);
		return -1;
	}
	
	fprintf(stderr, "OK\n");
	
	return fd;
}

int main(int argc, char **argv)
{
	int count = atoi(argv[argc - 1]);
	
	if (0 == count)
	{
		count = TEST_COUNT;
	}
	
	if (count > MAX_CONNECTION)
	{
		count = MAX_CONNECTION;
	}
	
	max_connections = count;
	
	memset(conn_pool, 0, sizeof(conn_pool));
	
	ev_base = event_init();
	
	conn_clock_handler(0, 0, 0);
	hb_clock_handler(0, 0, 0);
	
	event_base_loop(ev_base, 0);
	
	return 0;
}
